package algotithm.weekendrace.leetcode242.test2;

/**
 * @author 25068
 * @Date 2021/5/23 12:04
 */
class Solution {
    public int minSpeedOnTime(int[] dist, double hour) {
        if (dist.length - 1 > hour) {
            return -1;
        }
        int st = 1, ed = 1_000_000_1;
        while (st < ed) {
            int mid = (ed + st) / 2;
            double requireTime = calculateTime(dist, mid);
            if (requireTime > hour) {
                st = mid + 1;
            } else {
                ed = mid;
            }
        }
        return st;
    }

    private double calculateTime(int[] dist, double speed) {
        double time = 0;
        for (int i = 0; i < dist.length; ++i) {
            double curTime = dist[i] / speed;
            if (i == dist.length - 1) {
                time += curTime;
            } else {
                time += Math.ceil(curTime);
            }
        }
        return time;
    }
}

